package com.global.dao.impl;

import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.global.dao.ReportDao;
import com.global.model.Report;
import com.global.model.ReportCategory;

@Repository("reportDao")
public class ReportDaoImpl extends GenericDaoHibernate<Report, Long>  implements ReportDao {

	public ReportDaoImpl() {
		super(Report.class);
	}
	
	/**
	 * {@inheritDoc}
	 */
	public Report saveReport(Report report) {
		if(report!=null){
	        if (log.isDebugEnabled()) {
	            log.debug("report's id: " + report.getId());
	        }
	        getHibernateTemplate().saveOrUpdate(report);
	        getHibernateTemplate().flush();
		}
		
		return report;
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean saveBatchReport(List<Report> reportList) {
		if(reportList!=null){
			getHibernateTemplate().saveOrUpdateAll(reportList);
			getHibernateTemplate().flush();
			
			return true;
		}
		
		return false;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public List<Report> getReportList(ReportCategory reportCategory){
		if(reportCategory!=null){
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
	        queryParams.put("uniqueId", reportCategory.getId());
			return this.findByNamedQuery("findReportByReportCategory", queryParams);
		}

		return null;
	}
	
	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
    public List<Report> getReportList(boolean status){		
		StringBuilder hql = new StringBuilder();
		hql.append("from Report r where r.status = :status ");
		
		return getHibernateTemplate().findByNamedParam(hql.toString(), "status", status);
	}
	
	/**
	 * {@inheritDoc}
	 */
	public Report getReport(String uniqueName){
		if(StringUtils.isNotBlank(uniqueName)){
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
	        queryParams.put("uniqueName", uniqueName.toUpperCase());
			List<Report> list = this.findByNamedQuery("findReportByName", queryParams);
			if(!list.isEmpty()){
				return list.get(0);
			}
		}

		return null;
	}
}
